package com.yygo.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/6/15.
 */
@Entity
@Table(name = "loan_product")
@NamedEntityGraphs (
        @NamedEntityGraph( name = "movieWithActors", attributeNodes = {@NamedAttributeNode("movieActors")})
)
public class LoanProduct extends BaseObject {

    private static final long serialVersionUID = 4345356605625051185L;
    private Long id;
    private String productName;
    private float costRate;
    private float coef;
    private float crLL;
    private float crUL;
    private float dpLL;
    private float dpUL;
    private Long faLL;
    private Long faUL;
    private boolean second;
    private List<Integer> loanYears;
    private boolean deleted=false;

    private List<String> completeLoanMaterialNames;
    private List<String> loanMaterialNames;

    private Bank bank = new Bank();

    private boolean enabled;

  private Set<Long> bondIds =  new HashSet<>();
    private Set<BondCompany> bonds = new HashSet<>();

    public LoanProduct() {}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
  @Transient
  public Set<Long> getBondIds() {
    return bondIds;
  }

  public void setBondIds(Set<Long> bondIds) {
    this.bondIds = bondIds;
  }

   @Column(name = "product_name", length = 50)
    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Column(name = "cost_rate")
    public float getCostRate() {
        return costRate;
    }

    public void setCostRate(float costRate) {
        this.costRate = costRate;
    }

    @Column(name = "coef")
    public float getCoef() {
        return coef;
    }

    public void setCoef(float coef) {
        this.coef = coef;
    }

    @Column(name = "cr_ll")
    public float getCrLL() {
        return crLL;
    }

    public void setCrLL(float crLL) {
        this.crLL = crLL;
    }

    @Column(name = "cr_ul")
    public float getCrUL() {
        return crUL;
    }

    public void setCrUL(float crUL) {
        this.crUL = crUL;
    }

    @Column(name = "dp_ll")
    public float getDpLL() {
        return dpLL;
    }

    public void setDpLL(float dpLL) {
        this.dpLL = dpLL;
    }

    @Column(name = "dp_ul")
    public float getDpUL() {
        return dpUL;
    }

    public void setDpUL(float dpUL) {
        this.dpUL = dpUL;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    @Column(name = "deleted")
    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    @Column(name = "fa_ll")
    public Long getFaLL() {
        return faLL;
    }

    public void setFaLL(Long faLL) {
        this.faLL = faLL;
    }

    @Column(name = "fa_ul")
    public Long getFaUL() {
        return faUL;
    }

    public void setFaUL(Long faUL) {
        this.faUL = faUL;
    }

    @ElementCollection(targetClass=java.lang.Integer.class, fetch = FetchType.EAGER)
    @JoinTable(
            name="loan_product_year",
            joinColumns = {@JoinColumn(name="loan_product_id")}
    )
    @Column(name="year")
    public List<Integer> getLoanYears() {
        return loanYears;
    }

    public void setLoanYears(List<Integer> loanYears) {
        this.loanYears = loanYears;
    }

    @ElementCollection(targetClass=java.lang.String.class)
    @JoinTable(
            name="loan_product_complete_loan_material_names",
            joinColumns = {@JoinColumn(name="loan_product_id")}
    )
    @Column(name="material_name")
    public List<String> getCompleteLoanMaterialNames() {
        return completeLoanMaterialNames;
    }

    public void setCompleteLoanMaterialNames(List<String> completeLoanMaterialNames) {
        this.completeLoanMaterialNames = completeLoanMaterialNames;
    }

    @ElementCollection(targetClass=java.lang.String.class)
    @JoinTable(
            name="loan_product_loan_material_names",
            joinColumns = {@JoinColumn(name="loan_product_id")}
    )
    @Column(name="material_name")
    public List<String> getLoanMaterialNames() {
        return loanMaterialNames;
    }

    public void setLoanMaterialNames(List<String> loanMaterialNames) {
        this.loanMaterialNames = loanMaterialNames;
    }

    @ManyToOne
    @JoinColumn(name = "bank_id")
    public Bank getBank() {
        return bank;
    }

    public void setBank(Bank banck) {
        this.bank = banck;
    }

    @Column(name="is_second")
    public boolean isSecond() {
        return second;
    }

    public void setSecond(boolean isSecond) {
        this.second = isSecond;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "bond_product",
            joinColumns = @JoinColumn(name = "loan_product_id"),
            inverseJoinColumns = @JoinColumn(name = "bond_id"))
    public Set<BondCompany> getBonds() {
        return bonds;
    }

    public void setBonds(Set<BondCompany> bonds) {
        this.bonds = bonds;
    }

    @Column(name = "enabled")
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LoanProduct loanProduct = (LoanProduct) o;

        if (bank.equals(loanProduct.bank) && id.equals(loanProduct.id)) {
            return true;
        }

        return false;
    }

    public int hashCode() {
        int result = 3;
        result = 29 * result + (bank != null ? bank.hashCode() : 0);
        result = 57 * result + (productName != null ? productName.hashCode() : 0);
        return result;
    }



    public String toString() {
        return "LoanProduct{" +
                "bank=" + bank +
                "product name=" + productName +
                '}';
    }
}
